package com.example.fw;


import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


import com.example.tests.ContactData;

import java.util.Random;
import com.example.utils.SortedListOf;
public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
		
	}

	private SortedListOf<ContactData> cashedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cashedContacts == null) {
			rebuildCache();
			}
		return cashedContacts;
		}
	
	private void rebuildCache() {
		SortedListOf<ContactData> cashedContacts = new SortedListOf<ContactData>();
		
        manager.navigateTo().mainPage();
        
		List<WebElement> rows = getContactRows();
          for (WebElement row : rows) {
                String lastName = getLastNameInRow(row);
                cashedContacts.add(new ContactData().withLastName(lastName));
                }
          }

	public ContactHelper createContact(ContactData contact, boolean CREATION) {
		manager.navigateTo().mainPage();
		addContactPage();
		fillContactPage(contact, CREATION);
		submitContactCreation();
		returnToHomepage();
		rebuildCache();
		return this;
	}

	public ContactHelper deleteContact(int index) {
		editContact(index);
		submitContactDeletion();
		returnToHomepage();
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactData contact,
			boolean MODIFICATION) {
		editContact(index);
		fillContactPage(contact, MODIFICATION);
		updateContact();
		returnToHomepage();
		rebuildCache();
		return this;
	}
	
	
	//-----------------------------------------------------------------------------------------------------
	
	
	
	public ContactHelper addContactPage() {
		
	    click(By.linkText("add new"));
	    return this;
	}

	public ContactHelper fillContactPage(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getAdressFirst());
		type(By.name("home"), contact.getHomeTel());
		type(By.name("mobile"), contact.getMobileTel());
		type(By.name("work"), contact.getWorkTel());
		type(By.name("email"), contact.getEmailFirst());
		type(By.name("email2"), contact.getEmailSec());
		selectByText(By.name("bday"), contact.getBday());
		selectByNumber(By.name("bmonth"), contact.getBmonth());
		type(By.name("byear"), contact.getByear());
		type(By.name("address2"), contact.getAdressSec());
		type(By.name("phone2"), contact.getHomeSec());
		if (formType == CREATION) {
        	selectByText(By.name("new_group"), contact.contactGroup = selectRandomGroup()); 

        } 
        return this;

		
	}
	private String selectRandomGroup() {

		List<WebElement> options = driver.findElements(By.xpath("//form/select[3]/option"));
        Random rnd = new Random();
        return options.get(rnd.nextInt(options.size())).getText();
	}
	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
			}

	public ContactHelper returnToHomepage() {
		click(By.linkText("home"));
		return this;
			}

	public ContactHelper editContact(int index) {
		
		click(By.xpath("(//img[@title='Edit'])[" + (index+1) + "]"));
		return this;
	}

	

	public ContactHelper updateContact() {
	
		click(By.xpath("//input[@value='Update']"));
		return this;
	}

	

    private String getLastNameInRow(WebElement row) {

          return row.findElement(By.xpath("./td[2]")).getText();

}       



    private List<WebElement> getContactRows() {

    return driver.findElements(By.xpath("//tr[@name='entry']"));

}

    private void submitContactDeletion() {
		click(By.xpath("//input[@value='Delete']"));
	}	

	

	

    

}
